package com.example.customerservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.commonutils.JwtUtils;
import com.example.commonutils.R;
import com.example.commonutils.vo.OrderCustomerCommon;
import com.example.customerservice.client.MessageClient;
import com.example.customerservice.entity.OrderCustomer;
import com.example.customerservice.entity.vo.CustomerLoginVo;
import com.example.customerservice.entity.vo.CustomerVo;
import com.example.customerservice.service.OrderCustomerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-06-29
 */
@RestController
@RequestMapping("/customerservice/customer")
public class OrderCustomerController {
    @Autowired
    private OrderCustomerService customerService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MessageClient messageClient;
    //登录
    @PostMapping("login")
    public R login(@RequestBody CustomerLoginVo customerLoginVo){
        String token = customerService.login2(customerLoginVo);
        return R.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public R register(@RequestBody CustomerVo customerVo){
        String username = customerVo.getUsername();
        String password = customerVo.getPassword();
        String phone = customerVo.getPhone();
        String vaildcode = customerVo.getVaildcode();
        username = username.trim();
        password = password.trim();
        if (!StringUtils.isEmpty(username)){
            LambdaQueryWrapper<OrderCustomer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderCustomer::getUsername,username);
            int count = customerService.count(queryWrapper);
            if (count > 0){
                return R.error().message("该用户名太受欢迎了");
            }
            if (!StringUtils.isEmpty(password)){
                if (password.length()<6){
                    return R.error().message("密码长度应该在6-32位之间");
                }
            }else {
                return R.error().message("请填写密码");
            }
            if (!StringUtils.isEmpty(phone)){
                if (phone.length()!=11){
                    return R.error().message("手机号格式有误");
                }
                LambdaQueryWrapper<OrderCustomer> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(OrderCustomer::getPhone,phone);
                int count1 = customerService.count(queryWrapper1);
                if (count1 > 0){
                    return R.error().message("该手机号已被注册");
                }
            }else{
                return R.error().message("手机号不能为空");
            }
            if (!StringUtils.isEmpty(vaildcode)){
                messageClient.send(phone);
                String code = redisTemplate.opsForValue().get(phone);//从redis中获取的验证码
                if (!vaildcode.equals(code)){
                    return R.error().message("验证码错误");
                }
            }else {
                return R.error().message("验证码不能为空");
            }
            OrderCustomer orderCustomer = new OrderCustomer();
            orderCustomer.setUsername(username);
            orderCustomer.setPhone(phone);
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            orderCustomer.setPassword(password);
            customerService.save(orderCustomer);
        }else {
            return R.error().message("请填写用户名");
        }
        return R.ok().message("注册成功");
    }
    //根据token获取用户信息
    @GetMapping("getCustomerInfo")
    public R getCustomerInfo(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String customerId = JwtUtils.getMemberIdByJwtToken(request);
        //根据id获取用户信息
        OrderCustomer customer = customerService.getById(customerId);
        String username = customer.getUsername();
        return R.ok().data("customerInfo",customer);
    }
    //根据用户id获取用户信息
    @GetMapping("/getCustomerInfoById/{customerId}")
    public OrderCustomerCommon getCustomerInfoById(@PathVariable String customerId){
        OrderCustomer customer = customerService.getById(customerId);
        //把OrderCustomer对象里面的值赋值给OrderCustomerCommon
        OrderCustomerCommon orderCustomerCommon = new OrderCustomerCommon();
        BeanUtils.copyProperties(customer,orderCustomerCommon);
        return orderCustomerCommon;
    }
}

